package com.demo.statemachine.test1;

import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.transition.Transition;

public class StateMachineListener extends StateMachineListenerAdapter<States, Events> {
    @Override
    public void stateContext(StateContext<States, Events> stateContext) {
        if (stateContext.getStage() == StateContext.Stage.STATE_ENTRY) {
            System.out.println("Enter " + stateContext.getTarget().getId());
        } else if (stateContext.getStage() == StateContext.Stage.STATE_EXIT) {
            System.out.println("exit" );
        } else if (stateContext.getStage() == StateContext.Stage.STATEMACHINE_START) {
            System.out.println("machine started" );
        } else if (stateContext.getStage() == StateContext.Stage.STATEMACHINE_STOP) {
            System.out.println("machine stop" );
        }
    }

    @Override
    public void transition(Transition<States, Events> transition) {
        if (transition.getTarget().getId() == States.UNPAID) {
            System.out.println("订单创建，待支付");
            return;
        }

        if (transition.getSource().getId() == States.UNPAID
                && transition.getTarget().getId() == States.WAITING_FOR_RECEIVE) {
            System.out.println("用户完成支付，待收货");
            return;
        }
        if (transition.getSource().getId() == States.WAITING_FOR_RECEIVE
                && transition.getTarget().getId() == States.DONE) {
            System.out.println("用户已收货，订单完成");
            return;
        }
    }
}
